﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.Net.Nntp
{
    using System;
    using Phuse.Text;

    /// <summary>Represents a OVER command to be sent to an NNTP server, see
    /// <a href="http://www.ietf.org/rfc/rfc3977.txt">RFC 3977</a> 8.3.</summary>
    /// <remarks>For compatibility with existing implementations the command actually sent is XOVER.</remarks>
    /// <threadsafety static="true" instance="false"/>
    public sealed class OverCommand : SingleLineCommand<OverResponse>
    {
        private static readonly string[] SuccessResponseCodes = { "224" };

        /// <summary>Initializes a new instance of the <see cref="OverCommand"/> class.</summary>
        /// <exception cref="ArgumentNullException"><paramref name="messageId"/> equals <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><paramref name="messageId"/> contains white space.</exception>
        public OverCommand(string messageId) : this(" ", messageId, null, null)
        {
            CommandHelper.AssertContainsNoWhiteSpace("messageId", messageId);
        }

        /// <summary>Initializes a new instance of the <see cref="OverCommand"/> class.</summary>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="number"/> is smaller than or equal to 0.
        /// </exception>
        public OverCommand(long number) : this(" ", number, null, null)
        {
            if (number <= 0)
            {
                throw new ArgumentOutOfRangeException("number", ExceptionMessages.PositiveNumberRequired);
            }
        }

        /// <summary>Initializes a new instance of the <see cref="OverCommand"/> class.</summary>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="lowerNumber"/> and/or
        /// <paramref name="higherNumber"/> is smaller than or equal to 0.</exception>
        public OverCommand(long lowerNumber, long? higherNumber) : this(" ", lowerNumber, "-", higherNumber)
        {
            if (lowerNumber <= 0)
            {
                throw new ArgumentOutOfRangeException("lowerNumber", ExceptionMessages.PositiveNumberRequired);
            }

            if (higherNumber <= 0)
            {
                throw new ArgumentOutOfRangeException("higherNumber", ExceptionMessages.PositiveNumberRequired);
            }
        }

        /// <summary>Initializes a new instance of the <see cref="OverCommand"/> class.</summary>
        public OverCommand() : this(null, null, null, null)
        {
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>See <see cref="SingleLineCommandBase{T, U}.CreateResponse"/>.</summary>
        protected sealed override OverResponse CreateResponse(ResponseStream responseStream)
        {
            return new OverResponse(responseStream);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private OverCommand(params object[] args) :
            base(StringHelper.Format("XOVER{0}{1}{2}{3}", args), 2, SuccessResponseCodes)
        {
        }
    }
}
